//
//  NavigationController.m
//  TabBarController
//
//  Created by mac on 2018/9/11.
//  Copyright © 2018年 YangYang. All rights reserved.
//

#import "NavigationController.h"

@interface NavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation NavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationBar.barTintColor = [UIColor grayColor];
//    self.navigationBar.tintColor = [UIColor whiteColor];
//    self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    if (@available(iOS 15.0, *)) {
        NSLog(@"走了iOS15的方法");
        UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
        barApp.backgroundColor = [UIColor whiteColor];
        [barApp setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x333333)}];
        [UINavigationBar appearance].scrollEdgeAppearance = barApp;
        [UINavigationBar appearance].standardAppearance = barApp;
        
        UINavigationItem *navigationBarItem = [UINavigationItem new];
        [navigationBarItem.standardAppearance setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x333333)}];

        UITabBarAppearance *tabBarAppearance = [UITabBarAppearance new];
        tabBarAppearance.backgroundColor = [UIColor whiteColor];
        [tabBarAppearance.stackedLayoutAppearance.normal setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x333333)}];
        [tabBarAppearance.stackedLayoutAppearance.selected setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:152/255.0 green:79/255.0 blue:41/255.0 alpha:1.0]}];
        [UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
        [UITabBar appearance].standardAppearance = tabBarAppearance;
        [UITableView appearance].sectionHeaderTopPadding = 0;
    }
    
    
    
    // 获取系统自带滑动手势的target对象
    id target = self.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的pop滑动手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0 )
    {
        // 判断当前导航控制器的栈中是否有数据
        if (!self.childViewControllers.count) return;
        // 隐藏选项卡
        viewController.hidesBottomBarWhenPushed = YES;
        UIBarButtonItem* leftBarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
        viewController.navigationItem.leftBarButtonItem = leftBarItem;
        
    }
    [super pushViewController:viewController animated:animated];
}

-(void)popViewController
{
    [self popViewControllerAnimated:YES];
}


#pragma mark -- UIGestureRecognizerDelegate

// 什么时候调用：每次触发手势之前都会询问下代理，是否触发。
// 作用：拦截手势触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // 注意：只有非根控制器才有滑动返回功能，根控制器没有。
    // 判断导航控制器是否只有一个子控制器，如果只有一个子控制器，肯定是根控制器
    if (self.childViewControllers.count == 1 ) {
        // 表示用户在根控制器界面，就不需要触发滑动手势，
        return NO;
    }
    return YES;
}
//触发之后是否响应手势事件
//处理侧滑返回与UISlider的拖动手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    //如果手势是触摸的UISlider滑块触发的，侧滑返回手势就不响应
    if ([touch.view isKindOfClass:[UISlider class]]) {
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
